package com.kotlin.lib.coroutines

import kotlinx.coroutines.*

fun main() = runBlocking<Unit> {

    try {
        sum()
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
}


suspend fun sum(): Int = coroutineScope {

    val async1 = async<Int>(Dispatchers.Default) {
        try {
            delay(Long.MAX_VALUE)
            return@async 42
        } catch (e: Exception) {
            println("async 1 message:${e.message}")
        }
        0
    }


    val async2 = async<Int>(Dispatchers.Default) {
        println("async2 throw a exception")
        throw IllegalAccessException("error from async2")
    }
    return@coroutineScope async1.await() + async2.await()
}